home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Presentations / Presentations ’96 / Papers ’96 / Standard Template Library / Demo / main.cp < prev   
Encoding:
Text File  |  1996-06-21  |  3.4 KB  |  161 lines  |  [TEXT/SPM ]

  1. #define OLDROUTINELOCATIONS 0
  2. #include <ConditionalMacros.h>
  3. #include <Types.h> // For Str255
  4. #include <Dialogs.h> // For Alert
  5. #include <TextUtils.h> // For c2pstr
  6. #include <PLStringFuncs.h> // For PLstrcpy
  7. #include <ToolUtils.h> // For BitTst
  8. #include <Files.h>
  9.  
  10. #include <console.h>
  11.  
  12. #include <assert.h>
  13. #include <stdlib.h>
  14. #include <iostream.h>
  15.  
  16. #include "macString.h"
  17.  
  18. #define rUserAlert 129
  19.  
  20. typedef multimap<macString,macString, less<macString> > mmap;
  21. mmap m;
  22.  
  23. static void CatalogADirectory(long);
  24. static void DirID2FullPath(long, macString&);
  25. static Boolean NextFileInDirectory(long, HFileInfo&, macString&);
  26.  
  27. long    countFiles = 0;
  28. long    countDirectories = 0;
  29.  
  30. int main()
  31. {
  32.     console_options.ncols = 135;  // Works for 17" monitor.
  33.     console_options.nrows = 50;  // Works for 17" monitor.
  34.     cecho2file("Dupes.log", 0, stdout);
  35.  
  36.     cout << "Dupes: Examining the default volume..." << endl;
  37.     cout << endl;
  38.  
  39.     CatalogADirectory(fsRtDirID);    // start at the root
  40.     cout << endl;
  41.  
  42.     cout << "Total Directories = " << countDirectories << endl;
  43.     cout << "Total Files       = " << countFiles << endl;
  44.     cout << endl;
  45.  
  46.     // At this point all the files are in mmap, indexed by the file name
  47.     // Print out in sorted order
  48.     mmap::iterator i;
  49.     i = m.begin();
  50.     while (i != m.end ())
  51.     {
  52.         cout << (*i).first << " -> " << (*i).second << endl;
  53.         i++;
  54.     }
  55.     cout << endl;
  56.  
  57.     // Remove all the entries that have unique keys
  58.     mmap::iterator j;
  59.     i = m.begin();
  60.     while (i != m.end ())
  61.     {
  62.         const macString& key = (*i).first;
  63.         if (m.count(key) == 1)
  64.         {
  65.             j = i;
  66.             j++;
  67.             // cout << "Erasing "<< (*i).first << " -> " << (*i).second << endl;
  68.             m.erase(i);
  69.         }
  70.         else
  71.         {
  72.             for (int ii = m.count(key); ii > 0; ii--)
  73.             {
  74.                 // cout << "Saving "<< (*i).first << " -> " << (*i).second << endl;
  75.                 i++, j++;
  76.             }
  77.         }
  78.         i = j;
  79.     }
  80.     cout << endl;
  81.  
  82.     // print out the list of files with duplicate names
  83.     i = m.begin();
  84.     while (i != m.end ())
  85.     {
  86.         cout << (*i).first << " -> " << (*i).second << endl;
  87.         i++;
  88.     }
  89.     cout << endl;
  90.  
  91.     return EXIT_SUCCESS;
  92. }
  93.  
  94. static void CatalogADirectory(
  95.     long iDir)
  96. {
  97.     HFileInfo    fileInfo;
  98.     macString   sName;
  99.     macString    sDir;
  100.     macString   sFull;
  101.  
  102.     DirID2FullPath(iDir, sDir);
  103.     cout << sDir << endl;
  104.     
  105.     fileInfo.ioFDirIndex = 0;
  106.     fileInfo.ioVRefNum = 0;
  107.     while (NextFileInDirectory(iDir, fileInfo, sName)){
  108.         if (fileInfo.ioFlAttrib & ioDirMask)
  109.         {    // another directory, recurse
  110.             countDirectories++;
  111.             CatalogADirectory(fileInfo.ioDirID);
  112.         }
  113.         else
  114.         {    // file, add to catalog
  115.             countFiles++;
  116.             sFull = sDir + sName;
  117.             // cout << sDir << sName << endl;
  118.             cout << sFull << endl;
  119.             m.insert (pair<const macString, macString> (sName, sFull));
  120.         }
  121.     }
  122. }
  123.  
  124. static void DirID2FullPath(
  125.     long dirID,
  126.     macString& sFullPath)
  127. {
  128.     DirInfo    dirInfo;
  129.     Str255 str255Dir;
  130.     OSErr err;
  131.     macString sTemp;
  132.     
  133.     sFullPath = "";
  134.     dirInfo.ioDrParID = dirID;
  135.     dirInfo.ioNamePtr = str255Dir;
  136.     do {
  137.         dirInfo.ioVRefNum = 0;
  138.         dirInfo.ioFDirIndex = -1;
  139.         dirInfo.ioDrDirID = dirInfo.ioDrParID;
  140.         err = PBGetCatInfo((CInfoPBRec *)&dirInfo, false);
  141.         if (err)
  142.             Debugger();
  143.         sTemp = sFullPath;
  144.         sFullPath = str255Dir;
  145.         sFullPath += ":";
  146.         sFullPath += sTemp;
  147.     } while (dirInfo.ioDrDirID != fsRtDirID);  // The root, e.g. "pb:"
  148. }
  149.  
  150. static Boolean NextFileInDirectory(
  151.     long iDir,
  152.     HFileInfo& fileInfo,
  153.     macString& s)
  154. {
  155.     fileInfo.ioFDirIndex++;
  156.     fileInfo.ioDirID = iDir;
  157.     fileInfo.ioNamePtr = (unsigned char *)s;
  158.     return  PBGetCatInfo((CInfoPBRec *)&fileInfo, false) == noErr;
  159. }
  160.  
  161.